package com.smart.car.member.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.Md5Utils;
import com.smart.car.common.res.bean.ResponseResult;
import com.smart.car.common.res.constant.Constants;
import com.smart.car.common.res.entity.PointsInfo;
import com.smart.car.common.res.exception.BusinessException;
import com.smart.car.member.entity.MemberCard;
import com.smart.car.member.entity.MemberUser;
import com.smart.car.member.entity.MemberUserExample;
import com.smart.car.member.entity.MemberUserExample.*;
import com.smart.car.member.feign.PointsFeignService;
import com.smart.car.member.mapper.MemberCardMapper;
import com.smart.car.member.mapper.MemberUserMapper;
import com.smart.car.member.service.MemberUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: TODO
 * @author: zoutao
 * @date: 2021/4/15
 */
@Slf4j
@Service
public class MemberUserServiceImpl implements MemberUserService {

    @Resource
    private MemberUserMapper userMapper;
    @Resource
    private MemberCardMapper cardMapper;
    @Autowired
    private PointsFeignService pointsFeignService;


    @Override
    public List<MemberUser> list() {
        MemberUserExample userExample = new MemberUserExample();
        userExample.setOrderByClause("phone asc");
        List<MemberUser> members = userMapper.selectByExample(userExample);
        return members;
    }

    @Override
    public int bindMobile(String json) throws BusinessException {
        MemberUser user = JSONObject.parseObject(json, MemberUser.class);

        user.setState(Constants.IS_MEMBERVIP);
        int upstatus = userMapper.updateByPrimaryKeySelective(user);

        if(upstatus>0){
            MemberCard card = new MemberCard();
            card.setMemberId(user.getId());
            //雪花算法生成分布式唯一id
            Snowflake snowflake = IdUtil.getSnowflake(1, 1);
            long carNum = snowflake.nextId();
            card.setCardNum(String.valueOf(carNum));
            card.setStart(DateUtil.now());
            card.setEnds(DateUtil.nextMonth().toString());
            cardMapper.insertSelective(card);
            log.info("会员卡生成记录!");

            PointsInfo pointsInfo = new PointsInfo();
            pointsInfo.setMemberId(user.getId());
            pointsInfo.setCurQty("100");
            pointsFeignService.addPoints(JSONObject.toJSONString(card));
            log.info("会员积分增加记录!");
        }
        log.info("用户开通会员成功!");
        return upstatus;
    }

    @Override
    public MemberUser selectUserByPhone(String phone) {
        MemberUserExample example = new MemberUserExample();
        example.or().andPhoneLike("%"+phone+"%");
        //return userMapper.countByExample(example);
        return null;
    }

    //根据用户账号密码查询是否存在
    @Override
    public MemberUser queryUser(String username, String password) {
        MemberUserExample example = new MemberUserExample();
        Criteria criteria = example.createCriteria();
        criteria.andPhoneEqualTo(username);
        example.setOrderByClause("phone asc");
        //1.根据用户名查询用户是否存
        List<MemberUser> memberUsers = userMapper.selectByExample(example);
        if (memberUsers.size()==0 && memberUsers.isEmpty()) {
            log.info("用户不存在");
            return null;
        }else if(!StrUtil.equals(memberUsers.get(0).getPassword(),password)){
            return null;
        }else {
            return memberUsers.get(0);
        }
    }


    @Override
    public MemberUser getMember(String memberId) throws BusinessException {
        return userMapper.selectByPrimaryKey(memberId);
    }

    @Override
    public int insert(MemberUser record) {
        return userMapper.insert(record);
    }

    @Override
    public List<MemberUser> queryUser(String phone) {
        MemberUserExample example = new MemberUserExample();
        Criteria criteria = example.createCriteria();
        criteria.andPhoneEqualTo(phone);
        criteria.andPhoneIsNotNull();
        example.setOrderByClause("create_date desc limit 0,1");
        List<MemberUser>list = userMapper.selectByExample(example);
        return list;
    }

    @Override
    public MemberUser selectUserById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(MemberUser record) {
        MemberUserExample example = new MemberUserExample();
        Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(record.getId());
        return userMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return userMapper.deleteByPrimaryKey(id);
    }


    public static void main(String[] args) {
        String now = DateUtil.now();
        System.out.println(now);
        System.out.println(new DateTime());
        System.out.println(DateUtil.nextMonth().toString());
    }

}